﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using easysql;
using Model;
namespace Dal
{
    public class DepartmentDal : BaseDal<DepartmentModel>
    {
        private UserDal userDal = DalFactory.CreateUserDal();
        public DepartmentDal() : base("tb_department")
        {
        }
        public new void IDelById (int id) 
        {
            BaseDatabase db = dh.CreateDatabaseAndOpen();
            try
            {
                db.BeginTransaction();
                db.DelById(tbname, id);
                var count = db.Total<UserModel>("tb_user", null, Restrain.Eq("del", 1), Restrain.Eq("departmentId", id));
                if (count > 0)
                {
                    throw new IException("该部门下有<strong style=color:red>" + count+"</strong>个用户,不能删除");
                }
                db.CommitTranscation();
            }catch(Exception ex)
            {
                db.RollbackTranscation();
                throw ex;
            }
            finally
            {
                db.Dispose();
            }
        }
    }
}
